var addrNum=0;//地址信息标识
var addrId=getUrlVal("addrId");//获取用户修改地址后的地址Id
var endAddrId="";
var userMess="";
$(function(){
    //获取用户信息
    getUser();
    userMess=JSON.stringify(ym.user);
    getAddr();
});
/*查询地址信息*/
function getAddr(){
    if(addrId==undefined||addrId==null||addrId==""){
        addrId=0;
    }
    ym.ajax({
        url:'/shop/address/default',
        param:{
            receivingId:addrId,
            ymUser:userMess,
        },
        success:function(data){
            console.log(data)
            console.log("获取地址信息");
            if(data.obj==null||data.obj==undefined){
                $(".addAddr").removeClass("hide");
                addrNum=0;
                //查询商品信息
                getGoods();
            }else{
                $(".hasAddr").removeClass("hide");
                addrNum=1;
                endAddrId=data.obj.receivingId;
                $("#receiverName").html(data.obj.receiverName);
                $("#tel").html(data.obj.receiverPhone);
                var detailedAddress=(data.obj.detailedAddress).replace(/,/g, "");
                $(".contantAddr").html(detailedAddress+" "+data.obj.receiverAddress);
                //查询商品信息
                getGoods();
            }
        }
    });
};
//查询商品信息
function getGoods(){
    ym.ajax({
        url:'/shop/shopCar/getAll',
        param:{
            ymUser:userMess,
        },
        success:function(data){
            var html="";
            var sumMoney=0;
            var sumIntearge=0;
            var num=0;
             if(data.obj!=null&&data.obj.length>0){
                  for(var i=0;i<data.obj.length;i++){
                      var l=data.obj[i];
                      html+=' <li class="am-g am-g-collapse">'
                      html+='<span class="goodsImgDiv">'
                      html+='<img src="'+l.productPic+'" class="goodsImg" />'
                      html+='</span>'
                      html+='<span class="goodsMessDivs">'
                      html+='<span class="goodName">'
                      html+='<span class="goodsName">'+l.productName+'</span>'
                      html+='</span>'
                      html+='<span class="goodsNumDiv">'
                      var productPrice=(l.productPrice*0.01).toFixed(2);
                      var oneAllMoney=parseFloat(productPrice)*parseInt(l.shopCarProductNumber);
                      var oneAllIntearge=parseInt(l.productPoint)*parseInt(l.shopCarProductNumber);
                      sumMoney+=oneAllMoney;
                      sumIntearge+=oneAllIntearge;
                      html+='<span class="goodMoneyDiv ar-left">￥'+productPrice+'（'+l.productPoint+'积分）</span>'
                      num+=parseInt(l.shopCarProductNumber);
                      html+='<span class="goodsNum ar-right">X '+l.shopCarProductNumber+'</span>'
                      html+='</span></span></li>'
                  }
                  $(".goodsDiv").append(html);
                  sumMoney=sumMoney.toFixed(2);
                  $(".moneys").html("￥<span class='totalFee'>"+sumMoney+"</span>+<span class='totalIntearge'>"+sumIntearge+"</span>积分");
                  $("#num").html(num)
             }
        }
    });
}
/*选择地址*/
function choiceAddr(){
    window.location.href="address.html?pageTips=2";
    //pageTips标识从哪个页面跳转到选择地址页面，1从商品订单过去，2从购物车订单过去
}
/*提交订单*/
function buyGoods(){
    var nums=0;
    if(addrNum==0){
        $("#alertModal .am-modal-bd").html("请先选择配送地址！")
        $("#alertModal").modal();
        setTimeout(function () {
            $("#alertModal").modal('close');
        }, 2000);
    }else{
        var totalFee=$(".totalFee").html();
        var prices=$(".totalFee").html();
        var priceIntearge=$(".totalIntearge").html();
        totalFee=parseInt(totalFee*100);
        $('#myConfirm .am-modal-bd').html("您是否确认花费"+prices+"元+"+priceIntearge+"积分购买此商品？");
        var $confirm = $('#myConfirm');
        var confirm = $confirm.data('amui.modal');
        var onConfirm = function() {
            nums++;
            $(".masks").removeClass("hide");
            if(nums==1){
                //点击确认按钮调用事件
                ym.ajax({
                    url:'/shop/order/shopCarOrder',
                    param:{
                        totalFee:totalFee,
                        receivingId:endAddrId,
                        point:priceIntearge,
                        ymUser:userMess,
                    },
                    success:function(data){
                        if(data.res==1){
                            //调支付接口
                            var orderId=data.obj;
                            payType(orderId,totalFee);
                        }else{
                            $("#alertModal .am-modal-bd").html(data.resMsg);
                            $("#alertModal").modal();
                            setTimeout(function () {
                                $("#alertModal").modal('close');
                                $(".masks").addClass("hide");
                            }, 2000);

                        }
                    }
                });
            }
        };
        var onCancel = function() {
            //取消按钮调用事件
        };
        if (confirm) {
            confirm.options.onConfirm =  onConfirm;
            confirm.options.onCancel =  onCancel;
            confirm.toggle(this);
        } else {//重定义模态窗函数
            $confirm.modal({
                relatedElement: this,
                closeViaDimmer: false,//禁止点击遮罩层关闭
                onConfirm: onConfirm,
                onCancel: onCancel
            });
        }
    }
}
/*两秒后关闭弹窗*/
function setTime(){
    setTimeout(function () {
        $("#alertModal").modal('close');
        window.location.href="order.html";
    }, 2000);
}

